//
//  FWPageEmojiView.swift
//  DBS
//
//  Created by 王方伟 on 2017/4/24.
//  Copyright © 2017年 wfw. All rights reserved.
//

import UIKit

protocol PageEmojiViewDelegate : class {
    func numberOfSections(in pageEmojiView: FWPageEmojiView) -> Int
    func collectionView(_ pageEmojiView: FWPageEmojiView, numberOfItemsInSection section: Int) -> Int
    func collectionView(_ pageEmojiView: FWPageEmojiView, _ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
}

class FWPageEmojiView: UIView {
    weak var delegate : PageEmojiViewDelegate?
    fileprivate var collectionView : UICollectionView!
    
    fileprivate var titles : [String]
    fileprivate var style : FWTitleStyle
    fileprivate var isInTop : Bool
    fileprivate var layout : FWPageEmojiLayout
    
    fileprivate var pageControl : UIPageControl!
    fileprivate var currentIndxPath : IndexPath = IndexPath(item: 0, section: 0)
    fileprivate var titleView : FWTitleView!
    
    init(frame: CGRect, titles : [String], style : FWTitleStyle, isInTop : Bool, layout : FWPageEmojiLayout) {
        self.titles = titles
        self.style = style
        self.isInTop = isInTop
        self.layout = layout
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension FWPageEmojiView {
    fileprivate func setupUI (){
        let titleX : CGFloat = 0
        let titleY = isInTop ? 0 : bounds.height - style.titleHeight
        let titleW = bounds.width
        let titleH = style.titleHeight
        let titleFrame = CGRect(x: titleX, y: titleY, width: titleW, height: titleH)
        titleView = FWTitleView(frame: titleFrame, titles: titles, style:style)
        titleView.backgroundColor = style.backageGroudColor
        titleView.delegate = self;
        addSubview(titleView)
        
        let pageW = bounds.width
        let pageH : CGFloat = 20
        let pageX : CGFloat = 0
        let pageY = isInTop ? (bounds.height - pageH) : (bounds.height - titleH - pageH)
        let pageFrame = CGRect(x: pageX, y: pageY, width: pageW, height: pageH)
        pageControl = UIPageControl(frame: pageFrame)
        pageControl.numberOfPages = 4
        pageControl.isEnabled = false
        addSubview(pageControl)
        
        let collectionViewX : CGFloat = 0
        let collectionViewY = isInTop ? titleH : 0
        let collectionViewH = bounds.height - titleH - pageH
        let collectionViewW = bounds.width
        let collectionViewFrame = CGRect(x: collectionViewX, y: collectionViewY, width: collectionViewW, height: collectionViewH)
        collectionView = UICollectionView(frame: collectionViewFrame, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.randomColor()
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.isPagingEnabled = true
        collectionView.dataSource = self
        collectionView.delegate = self
        addSubview(collectionView)
        
    }
}


extension FWPageEmojiView : UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return delegate?.numberOfSections(in: self) ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let count = delegate?.collectionView(self, numberOfItemsInSection: section) ?? 0
        
        if section == 0 {
            pageControl.numberOfPages = (count - 1) / (layout.colCount * layout.rowCount) + 1
        }
        return count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        return delegate!.collectionView(self, collectionView, cellForItemAt: indexPath)
    }
}

extension FWPageEmojiView : UICollectionViewDelegate {
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        scrollViewEndScroll()
    }
    
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        if !decelerate {
            scrollViewEndScroll()
        }
    }
    
    fileprivate func scrollViewEndScroll(){
        let point = CGPoint(x: layout.sectionInset.left + 4 + collectionView.contentOffset.x, y: layout.sectionInset.top + 4)
        print(point)
        guard let indexPath = collectionView.indexPathForItem(at: point) else { return }
        if currentIndxPath != indexPath {
            let count = delegate?.collectionView(self, numberOfItemsInSection: indexPath.section) ?? 0
            pageControl.numberOfPages = (count - 1) / (layout.colCount * layout.rowCount) + 1
            titleView.onSlideContentView(from: currentIndxPath.section, to: indexPath.section, progress: 1.0)
            currentIndxPath = indexPath
        }
        pageControl.currentPage = indexPath.item / (layout.colCount * layout.rowCount)
    }
}

//MARK:对外暴露的方法
extension FWPageEmojiView {
    func register(cellClass : AnyClass?, identifier : String){
        collectionView.register(cellClass, forCellWithReuseIdentifier: identifier)
        
    }
    
    func register(nib : UINib?, identifier : String) {
        collectionView.register(nib, forCellWithReuseIdentifier: identifier)
    }
}

//MARK:<##>TitleViewDelegate
extension FWPageEmojiView : TitleViewDelegate {
    func onClickTitleView(_ titleView: FWTitleView, from: NSInteger, to: NSInteger) {
        var sumCount : Int = 0
        for index in 0..<to {//计算当前组之前有多少页
            let count = collectionView(collectionView, numberOfItemsInSection: index) % (layout.colCount * layout.rowCount) == 0 ? (collectionView(collectionView, numberOfItemsInSection: index) / (layout.colCount * layout.rowCount)) : (collectionView(collectionView, numberOfItemsInSection: index) / (layout.colCount * layout.rowCount) + 1 )
            sumCount += count
        }
        let offsetX = CGFloat(sumCount) * collectionView.frame.width
        collectionView.setContentOffset(CGPoint(x: offsetX, y: 0), animated: false)
        scrollViewEndScroll()
    }
}
